<template>
  <!-- 离职添加 -->
  <div class="contractInfo box">
    <el-divider content-position="left">{{
      $route.query.mode == "add" ? "添加离职" : "编辑离职"
    }}</el-divider>
    <div class="formBox">
      <el-form
        :model="form"
        :rules="rules"
        ref="addForm"
        label-width="auto"
        label-position="right"
      >
        <!-- <el-form-item label="部门" prop="departmentId">
          <el-select
            v-model="form.departmentId"
            placeholder="请选择"
            @change="selectDeaprtment"
            style="width: 100%"
          >
            <el-option
              v-for="item in departmentList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="领用人" prop="adminId">
          <el-select
            v-model="form.adminId"
            placeholder="请选择"
            style="width: 100%"
          >
            <el-option
              v-for="item in userList"
              :key="item.id"
              :label="item.username"
              :value="item.id"
            />
          </el-select>
        </el-form-item> -->
        <el-form-item label="离职日期" prop="departTime">
          <el-date-picker
            v-model="form.departTime"
            type="date"
            placeholder="请选择离职日期"
            value-format="YYYY-MM-DD"
            style="width: 100%"
          />
        </el-form-item>

        <el-form-item label="离职原因" prop="departReason" style="width: 100%">
          <el-input
            v-model="form.departReason"
            clearable
            type="textarea"
            rows="3"
            placeholder="请输入离职原因"
          ></el-input>
        </el-form-item>
        <el-form-item label="工作交接" style="width: 100%">
          <el-input
            v-model="form.handoverWork"
            clearable
            type="textarea"
            rows="5"
            placeholder="请输入交接工作内容"
          ></el-input>
        </el-form-item>
        <el-form-item label="备注" prop="remark" style="width: 100%">
          <el-input
            v-model="form.remark"
            clearable
            type="textarea"
            rows="3"
            placeholder="请输入备注内容"
          ></el-input>
        </el-form-item>
        <el-form-item label="附件">
          <sc-upload-file
            v-model="form.otherFileList"
            :limit="10"
            :multiple="false"
            tip="最多上传10个文件,单个文件不要超过20M"
          >
            <el-button type="primary" plain icon="el-icon-upload"
              >上传附件</el-button
            >
          </sc-upload-file>
        </el-form-item>
      </el-form>

      <!-- 审核流程项 -->
      <check-form-item
        ref="checkFormItem"
        @setCheckConfig="setCheckConfig"
        :field="this.form"
        :checkType="checkType"
        v-if="$route.query.mode == 'add'"
      ></check-form-item>
      <div class="button">
        <el-button type="primary" @click="save" :loading="isSaveing"
          >保 存</el-button
        >
        <el-button @click="cencel">取 消</el-button>
      </div>
    </div>
  </div>
</template>
<script>
import checkFormItem from "@/components/checkProcess/checkFormItem.vue"
import { queryTypeByDic } from "@/utils/getData"
export default {
  name: "leaveApplyAdd",
  data () {
    return {
      checkType: 28, //工作流的类型
      isSaveing: false,
      selectContractLoading: false,
      selectLoading: false,
      //表单数据
      form: {
        adminId: "", //跟进人id --登录时id
        departmentId: "",
        handoverWork: "", //	交接工作的内容
        departReason: "", //离职原因
        departTime: '', //离职日期
        remark: "", //备注
        exempt: "", //是否免审
        otherFileList: [], //附件
        workflowJsonList: [],
      },
      loanType: [], //借款类型
      //验证规则
      rules: {
        departmentId: [
          { required: true, message: '请选择部门', trigger: 'change' }
        ],
        adminId: [
          { required: true, message: '请选择离职人', trigger: 'change' }
        ],
        departReason: [{ required: true, message: "请输入离职原因" }],
        departTime: [
          {
            type: "date",
            required: true,
            message: "请选择离职日期",
            trigger: "change",
          },
        ],

      },
    }
  },
  components: {
    checkFormItem,
  },
  mounted () {
    this.userInfo = this.$TOOL.data.get('USER_INFO')
    if (this.$route.query.mode == 'edit') this.getDetail()

  },
  methods: {
    async getDetail () {
      let res = await this.$API.leaveApply.getLeaveApplyDetail.get({ id: this.$route.query.id })
      this.form = res.data
      this.form.otherFileList.forEach((item) => {
        item.name = item.fileName
      })
    },
    // 获取部门
    async getDepartmentList () {
      let { data } = await this.$API.department.getDepartmentList.get()
      this.departmentList = data
      this.form.departmentId = this.userInfo.departmentId

    },

    // 获取用户列表
    async getUserList (id) {
      let param = {
        limit: 1000,
        page: 1,
        departmentId: id
      }
      let { data } = await this.$API.user.getUserList.get(param)
      this.userList = data
      // this.form.adminId = this.userInfo.userId
    },

    // 选择部门
    selectDeaprtment (id) {
      this.form.adminId = ' '
      this.getUserList(id)
    },


    // 审批流配置
    setCheckConfig (list, exempt) {
      this.form.workflowJsonList = list
      this.form.exempt = exempt
    },


    async save () {
      this.$refs.addForm.validate(async (valid) => {
        if (valid) {
          this.isSaveing = true
          let userInfo = this.$TOOL.data.get("USER_INFO")
          this.form.departmentId = userInfo.departmentId
          this.form.adminId = userInfo.userId

          let fileType = ""
          this.form.otherFileList.map((item) => {
            if (item.url) {
              let arr = item.url.split(".")
              let type = arr[arr.length - 1]
              // fileType  1图片 2文件
              let imageType = "jpg/png/jpeg/svg"
              if (imageType.includes(type)) {
                fileType = 1
              } else {
                fileType = 2
              }
              item.fileType = fileType
              item.fileName = item.name
            }
          })

          let res = null
          let mode = this.$route.query.mode
          if (mode == "add") {
            res =
              await this.$API.leaveApply.addLeaveApply.post(
                this.form
              )
          } else if (mode == "edit") {
            delete this.form.status
            delete this.form.createTime
            delete this.form.updateTime
            res =
              await this.$API.leaveApply.updateLeaveApply.post(
                this.form
              )
          }
          this.isSaveing = false
          if (res.code == 200) {
            this.$emit("success", this.form, this.mode)
            this.closeSelectedTag(this.$route)
            this.$router.push({
              path: "/person/leaveApply",
            })
            this.$message.success("操作成功")
          }
        }
      })
    },

    // 取消客户添加
    cencel () {
      this.closeSelectedTag(this.$route)
      this.$router.push({
        path: "/person/leaveApply",
      })
    },
    //关闭tag
    closeSelectedTag (tag) {
      this.$store.commit("removeViewTags", tag)
      this.$store.commit("removeIframeList", tag)
      this.$store.commit("removeKeepLive", tag.name)
    },
  },
};
</script>
<style lang="scss" scoped>
.box {
  margin: 10px;
}

.formBox {
  padding: 30px;
  background: #fff;
  .el-form {
    width: 100%;
  }
}
.button {
  margin-top: 50px;
  padding-left: 60px;
}

:deep(.el-divider__text) {
  padding: 0 10px;
  font-size: 18px;
  // font-weight: 600;
  background-color: #f6f8f9;
}
</style>
